package com.jk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jk.model.*;
import com.jk.service.WJSService;
import org.apache.zookeeper.util.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.security.auth.Subject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("wjs")
public class WJSController {

    @Reference(check = false)
    private WJSService wjsService;

    //已卖出的商品查询
    @RequestMapping("findUserList")
    @ResponseBody
    public Map findUserList(Integer page, Integer rows, UserBean userBean, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserBean attribute = (UserBean) session.getAttribute(session.getId());
        Integer id = attribute.getId();
        Map userList = wjsService.findUserList(page, rows, userBean, id);
        return userList;
    }
    //商品加载
    @RequestMapping("shangpin")
    @ResponseBody
    public List<ProductBean> shangpin(){
        return wjsService.shangpin();
    }

    //买家查询
    @RequestMapping("yimai")
    @ResponseBody
    public Map yimai(Integer page, Integer rows, UserBean userBean, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserBean attribute = (UserBean) session.getAttribute(session.getId());
        Integer id = attribute.getId();
        Map yimai = wjsService.yimai(page, rows, userBean, id);
        return yimai;
    }

    //待付款
    @RequestMapping("daifukuan")
    @ResponseBody
    public Map daifukuan(Integer page, Integer rows, UserBean userBean, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserBean attribute = (UserBean) session.getAttribute(session.getId());
        Integer id = attribute.getId();
        Map daifukuan = wjsService.daifukuan(page, rows, userBean, id);
        return daifukuan;
    }

    //待发货
    @RequestMapping("daifahuo")
    @ResponseBody
    public Map daifahuo(Integer page, Integer rows, UserBean userBean, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserBean attribute = (UserBean) session.getAttribute(session.getId());
        Integer id = attribute.getId();
        Map daifahuo = wjsService.daifahuo(page, rows, userBean, id);
        return daifahuo;
    }

    //注册
    @RequestMapping("register")
    @ResponseBody
    public String register(UserBean userBean, HttpSession session) {
        //判断用户名是否重复
        UserBean queryUser = wjsService.queryUser(userBean);
        if (queryUser != null) {
            return "用户已存在";
        } else {
            wjsService.register(userBean);
            session.setAttribute("user", userBean);
            return "注册成功";
        }
    }

    //购买过的店铺
    @RequestMapping("shangdian")
    @ResponseBody
    public Map shangdian(Integer page, Integer rows, UserBean userBean, HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserBean attribute = (UserBean) session.getAttribute(session.getId());
        Integer id = attribute.getId();
        Map shangdian = wjsService.shangdian(page, rows, userBean, id);
        return shangdian;
    }

    //买家查询树
    @RequestMapping("findMaiList")
    @ResponseBody
    public List<MaiTree> findMaiList(){
        List<MaiTree> manageTree = wjsService.findMaiList();
        return manageTree;
    }

    //个人信息
    @RequestMapping("findgeren")
    @ResponseBody
    public Map findgeren(Integer page, Integer rows, UserBean userBean,HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserBean UserBean = (UserBean) session.getAttribute(session.getId());
        Integer id = UserBean.getId();
        Map findgeren = wjsService.findgeren(page, rows, userBean, id);
        return findgeren;
    }
    //收货地址
    @RequestMapping("dizi")
    @ResponseBody
    public Map dizi(Integer page, Integer rows, UserAcceptsite userAcceptsite,HttpServletRequest request){
        HttpSession session = request.getSession();
        UserBean attribute = (UserBean) session.getAttribute(session.getId());
        Integer id = attribute.getId();
        Map dizi = wjsService.dizi(page, rows, userAcceptsite, id);
        return dizi;
    }

    //跳转页面(注销)
    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.removeAttribute(session.getId());
        return "login";
    }

    //修改密码
    @RequestMapping("updatePassword")
    @ResponseBody
    public void updatePassword(String password, Integer userId) {
        wjsService.updatePassword(password,userId);
    }
    //验证密码
    @RequestMapping("codePassword")
    @ResponseBody
    public Map codePassword(String userPassword, HttpSession session) {
        return wjsService.codePassword(userPassword,session);
    }

}